package com.kuang.controller;

import com.kuang.pojo.Books;
import com.kuang.pojo.User;
import com.kuang.service.BooksService;
import com.kuang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    //controller 调service层
    @Autowired
    @Qualifier("UserServiceImpl")//指定别名
    private UserService userService ;

    //查询查询用户然后让其登上
    @RequestMapping("login")
    public String login(String user_code , String user_pwd , HttpSession session ,Model model ){
        List<User> users = userService.queryUserByCode(user_code, user_pwd);
        System.out.println(users.size());
        if (users.size() != 0){
            session.setAttribute("user_code" , user_code);
            System.out.println("5555555555555555");
            return "redirect:/book/allBook";
        }
        model.addAttribute("error" ,"输入的用户名不存在或者密码错误");
        return "forward:/index.jsp";
    }

    @RequestMapping("/goOut")
    public String goOut(HttpSession session ){
        session.removeAttribute("user_code");
        return "forward:/index.jsp";
    }

    @RequestMapping("/check")
    @ResponseBody
    public String check(String user_code , String user_pwd){
        System.out.println("进来了");
        User user = null;
        List<User> list = null;
        String msg = "";
        if (user_code != null){
            //admin 这个数据应该在数据库中查找
            user = userService.selectUser(user_code);
            if (user != null){
                msg = "ok";
            }else {
                msg = "账号不存在";
            }
        }

        if (user_pwd != null){
            //123456 这个数据应该在数据库中查找
            list = userService.queryUserByCode(user_code , user_pwd);
            if (list.size() != 0){
                msg = "ok";
            }else {
                msg = "密码错误";
            }
        }
        return  msg;
    }

}
